﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Ribbon_Ribbon_Gallery : System.Web.UI.UserControl
{
    private int _ItemsToShow = 3;
    private int _ItemsPerLine = 4;
    private bool _CanExpand = true;

    private System.Collections.Generic.List<Control> _Items;

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public System.Collections.Generic.List<Control> ContentTemplate
    {
        get { return _Items; }
        set { _Items = value; }
    }


    /// <summary>
    /// Items to be shown in the visible part of the gallery
    /// </summary>
    public int ItemsToShow
    {
        get { return this._ItemsToShow; }
        set { this._ItemsToShow = value; }
    }

    /// <summary>
    /// Defines the number of items to be shown per line in the gallery
    /// </summary>
    public int ItemsPerLine
    {
        get { return this._ItemsPerLine; }
        set { this._ItemsPerLine = value; }
    }

    /// <summary>
    /// Defines whether the user can expand or no the gallery
    /// </summary>
    public bool CanExpand
    {
        get { return this._CanExpand; }
        set { this._CanExpand = value; }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        this.div_expand.Visible = this._CanExpand;

        if (_Items != null)
        {
            int counter = 0;
            Panel pnl = new Panel();
            foreach (Control obj in _Items)
            {
                if ((counter < _ItemsToShow) && (this.PlaceHolder_itemsVisible.Controls.Count < _ItemsToShow))
                {
                    this.PlaceHolder_itemsVisible.Controls.Add(obj);
                    counter++;

                    if (counter == _ItemsToShow) counter = 0;
                }
                else
                {
                    if (counter == 0)
                    {
                        pnl = new Panel();
                        pnl.Style.Add("clear", "both");
                        this.PlaceHolder_menu.Controls.Add(pnl);
                    }

                    pnl.Controls.Add(obj);
                    counter++;
                    if (counter == _ItemsPerLine) { counter = 0; }
                }
                
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.div_expand.Attributes.Add("onclick", "SBRibbon.OpenGallery('" + this.div_item.ClientID + "', '" + this.div_menu.ClientID + "')");
        this.div_menu.Attributes.Add("onmouseover", "SBRibbon.KeepMenu('" + this.div_menu.ClientID + "')");
        this.div_menu.Attributes.Add("onmouseout", "SBRibbon.HideMenu('" + this.div_menu.ClientID + "')");
        this.div_menu.Attributes.Add("MenuOpener", this.div_menu.ClientID);
    }
}
